package edzest.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;

import edzest.dao.MemberDAO;
import edzest.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField uid;
	private JPasswordField pwd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateMember dialog = new UpdateMember( "0", 0, null );
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	// public UpdateMember() { 였는데
	public UpdateMember( final String mno, final int r, final JTable table ) {
		setTitle(":: 회원 수정 ::");
		// 회원 정보 수정을 위해
		// ManageMember에서 전달한 mno를 받기 위해
		// UpdateMember의 기본생성자 대신 
		// mno를 매개변수로 하는 새로운 생성자 작성
		
		// 또한, 회원정보 수정후  ManagerMember의 
		// JTable의 model도 변경해야 하므로
		// 수정해야할 행row과 JTable 객체도
		// 생성자 매개변수로 선언
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		
		uid = new JTextField();
		uid.setEnabled(false);
		uid.setColumns(10);
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		
		pwd = new JPasswordField();
		pwd.setColumns(16);
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		
		JRadioButton isMale = new JRadioButton("남자");
		isMale.setSelected(true);
		isMale.setFont(new Font("굴림", Font.BOLD, 12));
		
		JRadioButton radioButton_1 = new JRadioButton("여자");
		radioButton_1.setSelected(true);
		radioButton_1.setFont(new Font("굴림", Font.BOLD, 12));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(42)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
							.addGap(114)
							.addComponent(name, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
							.addGap(69)
							.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
							.addGap(32)
							.addComponent(radioButton_1, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
								.addGap(69)
								.addComponent(uid))
							.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
								.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
								.addGap(69)
								.addComponent(pwd, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(48, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(28)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(10)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(4)
							.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(11)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(42)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(2)
							.addComponent(isMale))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(2)
							.addComponent(radioButton_1)))
					.addContainerGap(33, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("수정완료");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 수정완료 버튼 클릭시
						// 수정할 값을 MemberVO에 저장
						MemberVO m = new MemberVO(
								mno, null,  // mno를 final로 해야 에러사라짐
								new String(pwd.getPassword()), 
								name.getText(), null, null);
						// MemberDAO 의 updateMember메서드 호출 후
						int cnt = new MemberDAO().updateMember(m);
						// 회원정보가 제대로 수정된 경우
						// 수정완료 메세지 창 출력
						if (cnt > 0) {
							JOptionPane.showMessageDialog(null, "수정완료!!");
							// UpdateMember에서 수정한 값을 
							// ManagerMember의 JTable에 적용해야 함
							// 이름 값만 변경됨
							table.setValueAt(name.getText(), r, 2);
							table.repaint(); // 변경사항 적용
							dispose(); // 현재창 닫기
							// JTable
						}
					}
				});
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
			{
				JButton notBtn = new JButton("수정취소");
				notBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
						// 수정최소 버튼 클릭시
						// 회원정보 수정 창 닫음
					}
				});
				notBtn.setActionCommand("Cancel");
				buttonPane.add(notBtn);
			}
		}
		
		// 회원번호를 이용해서 특정회원의 모든 정보를
		// select 한 후 MemberVO에 저장
		MemberVO m = new MemberDAO().selectOneMember(mno);
		
		// MemberVO의 값을 컴퍼넌트에 출력
		uid.setText( m.getUserid() );
		name.setText( m.getName() );
		pwd.setText( m.getPwd() );
		
	} // 생성자에 걸리는 괄호
}
